package com.dap.dubbo.rpc.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;
import com.dap.dubbo.rpc.cache.RegisterCache;
import com.dap.dubbo.rpc.config.RedisConfig;
import org.apache.commons.lang.StringUtils;
import org.mvel2.MVEL;
import org.mvel2.compiler.ExpressionCompiler;
import redis.clients.jedis.Jedis;

/**
 * 降级
 */
/*@Activate(group= Constants.CONSUMER)*/
public class DowngradeFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Jedis jedis = RedisConfig.getJedis();
         String strategy ="";
        if(null != jedis){
            strategy=jedis.get("INVOKE_STRATEGY");
        }
    if(StringUtils.isBlank(strategy)){
          if("wait".equals(strategy)){
              return invoker.invoke(invocation);
          }else if("reject".equals(strategy)){
              /**
               * 拒绝服务
               */
              return new RpcResult("拒绝服务");
          }else{
              ExpressionCompiler compiler = new ExpressionCompiler("11111");
              org.mvel2.compiler.CompiledExpression exp = compiler.compile();
              Object result = MVEL.executeExpression(exp);
              return new RpcResult(result);
          }
     }
        return  invoker.invoke(invocation);
    }

}

